var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    type:0,//0参团1团长分享
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      wx.setStorageSync("tmp_options", options)
    }
    //判断登陆
    var rdsession = wx.getStorageSync('rdsession');
    if (!rdsession) {
      //暂存参数

      wx.navigateTo({
        url: '/pages/usersq/usersq',
      })
      return false;
    }
    if(options){
      if(options.detail_id){
        this.setData({
          id:options.detail_id
        })
      }
    }
    this.getPintuanDetail();
    this.getPintuanVideo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/pingou-detail/pingou-detail?pid=' + user.user_id+'&detail_id='+page.data.id,
    }
  },
  //获取拼团详情
  getPintuanDetail:function(){
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.order.video_pintuan_detail,
      method: 'POST',
      data: {
        detail_id: page.data.id
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            info: res.data,
          });
          if(res.data.kaituan_uid==user_info.user_id){
            page.setData({
              type:1,
            })
          }

        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.msg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },

  //参团调起支付
  joinPintuan:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.order.video_joinpintuan,
      method: 'POST',
      data: {
        detail_id: page.data.id
      },
      success: function (res) {
        if (res.errcode == 0) {
          let order_id = res.data
          //订单支付
          app.request({

            url: api.order.get_pay_data,
            method: "post",
            data: {
              order_id: order_id,
              pay_type: 'WECHAT_PAY'
            },
            success: function (result) {
              wx.hideLoading();
              if (result.errcode == 0) {
                var pay_data = result.data;
                wx.requestPayment({
                  timeStamp: pay_data.timeStamp,
                  nonceStr: pay_data.nonceStr,
                  package: pay_data.package,
                  signType: pay_data.signType,
                  paySign: pay_data.paySign,
                  success: function (res) {
                    wx.showToast({
                      title: '订单支付成功',
                      icon: 'success'
                    });
                    setTimeout(function(){
                      wx.redirectTo({
                        url: "../class-order/class-order?tab=1"
                      })
                    })
                   
                  },
                  fail: function (res) {
                    wx.showToast({
                      title: '订单未支付',
                      image: '/images/icon-warning.png'
                    });
                  }
                });
              } else {
                wx.hideLoading();
                wx.showModal({
                  title: '提示',
                  content: result.errmsg,
                  showCancel: false
                });
              }
            }
          });
        }else{
          wx.showModal({
            title: '提示',
            content: res.data,
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },

  //推荐拼团课程
  getPintuanVideo:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.video.pintuan_video,
      method: 'POST',
      data: {
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            pintuan_video: res.data,
          });
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.msg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  }
})